package com.seeyon.dee.codelib;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Hex;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 结果集处理工具类
 * @Copyright Beijing Seeyon Software Co.,LTD
 */
public class StrKitTC {

    public static String str(Object o) {

        if(o == null) {
            return "";
        }
        if(o instanceof String) {
            return (String)o;
        }
        return o.toString();
    }




    /*字符串MD5加密*/
    public static String stringToMD5(Object param) {
        if (param == null) {
            return "";
        } else {
            String result = null;
            MessageDigest md5 = null;
            try {
                md5 = MessageDigest.getInstance("MD5");
                byte[] input = str(param).getBytes(StandardCharsets.UTF_8);
                byte[] output = md5.digest(input);
                result = Hex.encodeHexString(output).toUpperCase();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return result;
        }
    }


    public static String getMd5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte[] b = md.digest();

            int i = 0;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


    /**
     * 获取同程token
     * @param account
     * @param password
     * @return
     */
    public static String getAccessToken(String account,String password){
        //获取UUId
        String uuid = UUID.randomUUID().toString();
        HashMap<String, Object> userInfo = new HashMap<>();
        userInfo.put("account",account);
        userInfo.put("password",password);
        Map<String, Object> parm = new HashMap<>();
        parm.put("isEncrypt",false);
        parm.put("random",uuid);
        parm.put("timestamp",new Date().getTime());
        parm.put("param", userInfo);
        String s1 = JSONObject.toJSONString(parm);
        return s1;
    }

//    public static Document listToDocument(List<Map<String, Object>> dataList){
//        Document documents = TransformFactory.getInstance().newDocument("root");
//        Element root = documents.getRootElement();
//        Element data = documents.createElement("data");
//
//        String mCount = (String.valueOf(dataList.size()));
//        String mTotalCount = (String.valueOf(dataList.size()));
//        if(dataList.size() > 0){
//            for (int i = 0; i < dataList.size(); i++) {
//                Map<String, String> accountantMap = dataList.get(i);
//                Element row = documents.createElement("row");
//                def key,value;
//                Element keyElement = null;
//                for (Map.Entry<String, String> entry : accountantMap.entrySet()) {
//                    key = entry.getKey().toString();
//                    value = entry.getValue() + "";
//                    keyElement = documents.createElement(key, value);
//                    row.addChild(keyElement);
//                }
//                data.addChild(row);
//            }
//            data.setAttribute("count", mCount);
//            data.setAttribute("totalCount", mTotalCount);
//            root.addChild(data);
//            return documents;
//        }
//    }
}
